package org.wattdepot.cli;

import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.util.tstamp.Tstamp;
//import org.wattdepot.resource.sensordata.jaxb.SensorData;

/**
 * Prints information about a single sensor data instance.
 * @author Edwin Lim, Wing Cheung Ma
 * 
 */
public class SensorData extends CommandLineInterface implements WattDepotCliCommand {

  /**
   * Gets input from the user and finds the sensor data according to the time instance.
   * Note that some inputs will return with an exception because there is no data.
   * @param args a list of input info
   * @return a string with the sensor data
   */
  @Override
  public String doCommand(List<String> args) {
    
    String timeStamp = args.get(1);
    String sourceName = args.get(0);
    
    Source source = null;
    try {
      source = searchSource(sourceName);
    }
    catch (WattDepotClientException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    
    if (source == null) {
      return sourceNotFound;
    }
    
    org.wattdepot.resource.sensordata.jaxb.SensorData sensorData = null;
    XMLGregorianCalendar now = null;
    
    try {
      now = Tstamp.makeTimestamp(timeStamp);
    }
    catch (Exception e) {
      System.out.println("should never happen.");
      return timeProblem;
    }
    
    try {
      try {
        sensorData = client.getSensorData(source.getName(), now);
      }
      catch (NotAuthorizedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (BadXmlException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (MiscClientException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    catch (ResourceNotFoundException notFound) {
      return sourceNotFound;
    }
    
    if (sensorData == null) {
      return "unable to retrieve sensor data";
    }
    
    StringBuffer sb = new StringBuffer(255);
    String sensorSource = sensorData.getSource();
    int i = sensorSource.indexOf("SIM");   
    System.out.println("Tool: " + sensorData.getTool());
    System.out.println("Source: " + sensorSource.substring(i));
    
    if (source.isSetProperties()) {
      for (org.wattdepot.resource.property.jaxb.Property property : 
        sensorData.getProperties().getProperty()) {
        sb.append(" [");
        sb.append(property.getKey());
        sb.append(", ");
        Double power = Double.parseDouble(property.getValue());
        sb.append(power);
        sb.append("] ");
      }
    }
    else {
      sb.append("none");
    }
    //sb.append(properties);
    String properties = sb.toString();
    
    properties = "Properties: " + properties;
    
    return properties;
  }

}
